package com.kingyea.mobilepolice.apimanage.utils.ws;

import org.apache.cxf.Bus;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.service.model.BindingInfo;
import org.apache.cxf.service.model.BindingOperationInfo;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class UploadTest {

    public static void main(final String[] args) {
        final String xmlTemplate = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r" + "<Q_Message>\r"
                + "  <SenderID>%s</SenderID>\r" + "  <ServiceID>%s</ServiceID>\r" + "  <ServiceDescript>\r"
                + "    <ServiceName>upload</ServiceName>\r" + "    <File md5=\"%s\" size=\"%s\">%s</File>\r"
                + "  </ServiceDescript>\r" + "</Q_Message>";

        final String senderID = "C00-00000015";
        final String serviceId = "S10-00000049";

        final String md5 = "12121212121212";
        final int size = 121212;
        final String fileName = "test.zip";

        final String xml = String.format(xmlTemplate, senderID, serviceId, md5, size, fileName);

        System.out.println(xml);

        final JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
        final Client client = clientFactory.createClient("http://localhost:8782/services/Hello?wsdl");
        final Bus bus = client.getBus();
        bus.setProperty("mtom-enabled", "true"); // 开启 MTOM ，不开启附件在soap报文中将使用 base64
        // 编码转成文本后上传，webservcie最大支持的文本为134217728
        Object[] result = null;
        final List<Object> attachments = new ArrayList<>();
        try {

//            String filePath = "f:\\head.jpg";
//            DataHandler dataHandler = new DataHandler(new FileDataSource(filePath));


            final Object attachmentWrapper = Thread.currentThread().getContextClassLoader()
                    .loadClass("com.kingyea.mobilepolice.apimanage.test.service.AttachmentWrapper").newInstance();
            final Method m1 = attachmentWrapper.getClass().getMethod("setFileName", String.class);
            final Method m2 = attachmentWrapper.getClass().getMethod("setFileExtension", String.class);
            final Method m3 = attachmentWrapper.getClass().getMethod("setFile", DataHandler.class);
            m1.invoke(attachmentWrapper, "test");
            m2.invoke(attachmentWrapper, "zip");
            m3.invoke(attachmentWrapper, new DataHandler(new FileDataSource(new File("D:/test/test.zip"))));


            attachments.add((attachmentWrapper));

            final Object[] parameters = new Object[]{
                    "D:/test/test.zip", attachments
            };
            // 获取op
            final Endpoint endpoint = client.getEndpoint();
            final BindingInfo info = endpoint.getEndpointInfo().getBinding();

//            SOAPBinding binding = (SOAPBinding) endpoint.getBinding();
//            binding.setMTOMEnabled(true);

            final Collection<BindingOperationInfo> operations = info.getOperations();
            QName op = null;
            for (final BindingOperationInfo bindingOperationInfo : operations) {
                final QName opQName = bindingOperationInfo.getOperationInfo().getName();
                if (opQName.getLocalPart().equals("upload")) {
                    op = opQName;
                    break;
                }
            }
            result = client.invoke(op, parameters);
        } catch (final Exception e) {
            e.printStackTrace();
        }
        System.out.println(result[0]);
    }


//    public static void main(final String[] args) {
//        final String xmlTemplate = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r" + "<Q_Message>\r"
//                + "  <SenderID>%s</SenderID>\r" + "  <ServiceID>%s</ServiceID>\r" + "  <ServiceDescript>\r"
//                + "    <ServiceName>upload</ServiceName>\r" + "    <File md5=\"%s\" size=\"%s\">%s</File>\r"
//                + "  </ServiceDescript>\r" + "</Q_Message>";
//
//        final String senderID = "C00-00000015";
//        final String serviceId = "S10-00000049";
//
//        final String md5 = "12121212121212";
//        final int size = 121212;
//        final String fileName = "test.zip";
//
//        final String xml = String.format(xmlTemplate, senderID, serviceId, md5, size, fileName);
//
//        System.out.println(xml);
//
//        final JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
//        final Client client = clientFactory.createClient("http://192.168.0.235:7777/dataex/upload_noland?wsdl");
//        final Bus bus = client.getBus();
//        bus.setProperty("mtom-enabled", "true"); // 开启 MTOM ，不开启附件在soap报文中将使用 base64
//        // 编码转成文本后上传，webservcie最大支持的文本为134217728
//        Object[] sysvo = null;
//        final List<Object> attachments = new ArrayList<>();
//        try {
//
//            final Object attachmentWrapper = Thread.currentThread().getContextClassLoader()
//                    .loadClass("com.kingyea.esb.shareservice.bigdata.cross.AttachmentWrapper").newInstance();
//            final Method m1 = attachmentWrapper.getClass().getMethod("setFileName", String.class);
//            final Method m2 = attachmentWrapper.getClass().getMethod("setFileExtension", String.class);
//            final Method m3 = attachmentWrapper.getClass().getMethod("setFile", DataHandler.class);
//            m1.invoke(attachmentWrapper, "test");
//            m2.invoke(attachmentWrapper, "zip");
//            m3.invoke(attachmentWrapper, new DataHandler(new FileDataSource(new File("bin/test.zip"))));
//            attachments.add(attachmentWrapper);
//
//            final Object[] parameters = new Object[] {
//                    xml, attachments
//            };
//            // 获取op
//            final Endpoint endpoint = client.getEndpoint();
//            final BindingInfo info = endpoint.getEndpointInfo().getBinding();
//            final Collection<BindingOperationInfo> operations = info.getOperations();
//            QName op = null;
//            for (final BindingOperationInfo bindingOperationInfo : operations) {
//                final QName opQName = bindingOperationInfo.getOperationInfo().getName();
//                if (opQName.getLocalPart().equals("upload")) {
//                    op = opQName;
//                    break;
//                }
//            }
//            sysvo = client.invoke(op, parameters);
//        } catch (final Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println(sysvo[0]);
//    }
}
